package com.inspector.app.ui.flight.statusBar;



import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.inspector.app.R;
import com.inspector.app.util.CommonUtil;
import com.inspector.app.util.ScreenUtils;

import java.util.List;

/**
 * 飞行状态栏弹窗
 * {"北京时间", "海拔高度", "速度", "航向", "到达点", "剩余距离",
 * "已飞距离", "预达时刻", "最终距离", "最终预达时刻",
 * "当前经度","当前纬度", "偏航距离", "电量", "UTC",
 * "卫星数", "磁航向", "磁差","计时器"};
 *
 * @author CodeK 2023/8/23
 */
public class FlightStatusBarPopup extends PopupWindow {

    private MapSetTitleAdapter mapSetTitleAdapter;
    public FlightStatusBarPopup(Context context, List<FlightStatusBar> items, Object viewTag, OnItemClickListener itemClickListener) {
        super(context);
        View contentView = LayoutInflater.from(context).inflate(R.layout.flight_statusbar_popup, null, false);

        RecyclerView rvTemplate = contentView.findViewById(R.id.rv_map_set_title);
        LinearLayout ll_bg = contentView.findViewById(R.id.ll_bg);

        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        mapSetTitleAdapter = new MapSetTitleAdapter(items, viewTag);
        mapSetTitleAdapter.setOnItemClickListener(itemClickListener);
        rvTemplate.setAdapter(mapSetTitleAdapter);
        rvTemplate.setLayoutManager(layoutManager);
        rvTemplate.setItemAnimator(new DefaultItemAnimator());
        rvTemplate.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.set(1, 1, 1, 1);
            }
        });
        DividerItemDecoration decoration = new DividerItemDecoration(context, DividerItemDecoration.VERTICAL);
        decoration.setDrawable(ContextCompat.getDrawable(context, R.drawable.divider_line));
        rvTemplate.addItemDecoration(decoration);

        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        //判断平板还是手机  手机左侧显示0.7宽度 白色背景  平板中间显示0.5 黑色半透明背景色


        if (CommonUtil.isTabletDevice(context)){
            ll_bg.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_status_top));
            setWidth((int) (ScreenUtils.getScreenWidth(context) * 0.5));
        } else {
            ll_bg.setBackground(ContextCompat.getDrawable(context, R.drawable.shape_bg_white));
            setWidth((int) (ScreenUtils.getScreenWidth(context) * 0.7));
        }

        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setContentView(contentView);
    }

    public void showAtLocation(View view) {
        if (CommonUtil.isTabletDevice(view.getContext())){
            showAtLocation(view, Gravity.CENTER, 0, 0);
        } else {
            showAtLocation(view, Gravity.LEFT, 0, 0);
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    public void updateAdapter() {
        if (isShowing() && mapSetTitleAdapter != null) {
            mapSetTitleAdapter.notifyDataSetChanged();
        }
    }


    //定义点击回调接口
    public interface OnItemClickListener {
        void onItemClick(FlightStatusBar item, int position, Object viewTag);
    }

}
